package com.axend.aerosense.common.adapter;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.BindingAdapter;

import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.common.config.Gobal;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.axend.aerosense.common.R;
import com.bumptech.glide.signature.ObjectKey;

/**
 * 应用模块:adapter
 * <p>
 * 类描述: 自定义BindingAdapter
 * <p>
 *
 * @author darryrzhoong
 * @since 2020-01-28
 */
public class CommonBindingAdapters {
    @BindingAdapter("imageUrl")
    public static void loadImage(ImageView imageView, String url) {
        try{
            if (!TextUtils.isEmpty(url)) {
                Glide.with(imageView.getContext())
                        .load(url)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(40)))
                        .into(imageView);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @BindingAdapter("imageBgUrl")
    public static void loadBgImage(ViewGroup viewGroup, String url) {
        try{
            if (!TextUtils.isEmpty(url)) {
                Glide.with(viewGroup.getContext())
                        .asBitmap()
                        .load(url)
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(@NonNull Bitmap resource,
                                                        @Nullable Transition<? super Bitmap> transition) {
                                Drawable drawable = new BitmapDrawable(resource);
                                viewGroup.setBackground(drawable);
                            }
                        });
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @BindingAdapter("imageWrapUrl")
    public static void loadWrapImage(ImageView imageView, String url) {
        try{
            if (!TextUtils.isEmpty(url)) {
                Glide.with(imageView.getContext())
                        .load(url)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(40)))
                        .into(imageView);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @BindingAdapter("imageCircleUrl")
    public static void loadCircleImage(ImageView imageView, String url) {
        try{
            if (!TextUtils.isEmpty(url)) {
                Glide.with(imageView.getContext())
                        .load(url)
                        .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                        .into(imageView);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @BindingAdapter("imageCircleHeadUrl")
    public static void imageCircleHeadUrl(ImageView imageView, String url) {
        try{
            if (!TextUtils.isEmpty(url)) {
                Glide.with(imageView.getContext())
                        .load(url)
                        .error(R.mipmap.common_head) //异常时候显示的图片
                        .placeholder(R.mipmap.common_head) //加载成功前显示的图片
                        .fallback(R.mipmap.common_head) //url为空的时候,显示的图片
                        .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                        .into(imageView);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void imageCircleHeadUrl(ImageView imageView, String url, Boolean b) {
        try{
            if (!TextUtils.isEmpty(url)) {
                String updateTime = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.IMAGE_ICON_SIGN, "1");
                Glide.with(imageView.getContext())
                        .load(url)
                        .error(R.mipmap.common_head) //异常时候显示的图片
                        .placeholder(R.mipmap.common_head) //加载成功前显示的图片
                        .fallback(R.mipmap.common_head) //url为空的时候,显示的图片
                        .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                        .signature(new ObjectKey(updateTime))
                        .into(imageView);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @BindingAdapter("android:visibility")
    public static void setVisibility(View view, boolean value) {
        try{
            view.setVisibility(value ? View.VISIBLE : View.GONE);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
